//
//  visitorView.swift
//  weibo
//
//  Created by Qing on 2017/5/1.
//  Copyright © 2017年 成哥哥. All rights reserved.
//

import UIKit
import SnapKit


/// 访客视图 - 处理用户未登录时的界面显示。
class visitorView: UIView {
    

    
    //MARK: - 设置访客视图可重用函数，改变显示图标和文字提示。
    
    /// 设置视图信息
    ///
    /// - Parameters:
    ///   - imageName: 图片名字，首页设置为nil
    ///   - title: 提示消息文字
    func setUpInfo(imageName: String?, title: String) {
        
        msgLable.text = title
        
        guard let imaName = imageName else {
            
            startAnim()
            
            return
        }
        
        houseView.image = UIImage(named: imaName)
        
        backCircle.isHidden = true
    }
    
    /// 开启首页转轮动画
    fileprivate func startAnim() {
        
        let anim = CABasicAnimation(keyPath: "transform.rotation.z")
        anim.toValue = 2 * Float.pi

        anim.duration = 20
        anim.repeatCount = MAXFLOAT
        anim.isRemovedOnCompletion = false
        backCircle.layer.add(anim, forKey: nil)
    }
    
//    initWithFrame 是UIView的指定构造函数
//    使用纯代码开发的入口
    override init(frame: CGRect) {
        
        //使用父类的方法初始化。
        super.init(frame: frame)
        
        //在代码入口和SB入口都运行 ，就都可以显示了。否则只会从SB入口显示。
        
        setupView()

    }
    
//    initWithCoder是使用storyboard 和 XIB 开发加载的函数 。
//    使用storyboard入口。
    required init?(coder aDecoder: NSCoder) {
        
//        如果使用SB开发，调用这个视图，会直接崩溃。他是阻止使用SB。
//        fatalError("init(coder:) has not been implemented")
//        coder是外部参数名，aDEcoder是内部参数名。
        super.init(coder: aDecoder)

        //在代码入口和SB入口都运行 ，就都可以显示了。否则只会从SB入口显示。
        setupView()
        
    }
    
//    MARK: - 添加各类UI控件
//    小房子旁边的圆圈
     fileprivate lazy var backCircle = UIImageView(image: UIImage(named: "visitor_icon_circle"))
    
//    遮罩圆圈下方的图片，约束出现问题。先不管，最后再来处理
//    fileprivate lazy var coverImage = UIImageView(image: UIImage(named: "cover"))
    
//    小房子
    fileprivate lazy var houseView = UIImageView(image: UIImage(named: "visitor_icon_home"))
    
//    消息文字
    fileprivate lazy var msgLable: UILabel = {
        
        let msg = UILabel()
        msg.text = ""
        msg.textAlignment = .center
        msg.textColor = UIColor.darkGray
        msg.font = UIFont.boldSystemFont(ofSize: 14.0)
        msg.numberOfLines = 0
        return msg
    
    }()
    
//    登录按钮
     lazy var loginBtn = UIButton(type: UIButtonType.custom, image: "visitor_icon_loginButton_image", title: "登录", stated: UIControlState.normal)
    
//    注册按钮
     lazy var registerBtn = UIButton(type: UIButtonType.custom, image: "visitor_icon_loginButton_image", title: "注册", stated: UIControlState.normal)
    
}

// MARK: - 加载UI控件
extension visitorView {
    /// 创建访客视图
    fileprivate func setupView(){
        
//        1.加载视图控件
        addSubview(houseView)
        addSubview(backCircle)
//        addSubview(coverImage)
        addSubview(msgLable)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
//        访问界面控件约束布局
//        houseView.translatesAutoresizingMaskIntoConstraints = false
//        backCircle.translatesAutoresizingMaskIntoConstraints = false
//        msgLable.translatesAutoresizingMaskIntoConstraints = false
//        loginBtn.translatesAutoresizingMaskIntoConstraints = false
//        registered.translatesAutoresizingMaskIntoConstraints = false
//        跟上面一样的效果
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
//MARK: - 控件约束
//        1> 小房子旁边的圆圈 约束
//        addConstraint(NSLayoutConstraint(item: backCircle, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: backCircle, attribute: .centerY , relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1.0, constant: -30))
        backCircle.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-30)
        }
        
        
//        2> 小房子 约束
//        addConstraint(NSLayoutConstraint(item: houseView, attribute: .centerX, relatedBy: .equal, toItem: backCircle, attribute: .centerX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: houseView, attribute: .centerY, relatedBy: .equal, toItem: backCircle, attribute: .centerY, multiplier: 1.0, constant: 0))
        houseView.snp.makeConstraints { (make) in
            make.centerY.equalTo(backCircle.snp.centerY)
            make.centerX.equalTo(backCircle.snp.centerX)
        }
        
        
//        3> 消息文字 约束
//        addConstraint(NSLayoutConstraint(item: msgLable, attribute: .centerX, relatedBy: .equal, toItem: houseView, attribute: .centerX, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: msgLable, attribute: .top, relatedBy: .equal, toItem: houseView, attribute: .bottom, multiplier: 1.0, constant: 30))
//        addConstraint(NSLayoutConstraint(item: msgLable, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: 200))
//        addConstraint(NSLayoutConstraint.init(item: msgLable, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 36))
        msgLable.snp.makeConstraints { (make) in
            make.centerX.equalTo(houseView.snp.centerX)
            make.top.equalTo(houseView.snp_bottom).offset(30)
            make.width.equalTo(200)
            make.height.equalTo(36)
        }
        
//        4> 登录按钮 约束
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .left, relatedBy: .equal, toItem: msgLable, attribute: .left, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .top, relatedBy: .equal, toItem: msgLable, attribute: .bottom, multiplier: 1.0, constant: 20))
//        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: 70))
//        addConstraint(NSLayoutConstraint.init(item: loginBtn, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 36))
        loginBtn.snp.makeConstraints { (make) in
            make.left.equalTo(msgLable.snp_left)
            make.top.equalTo(msgLable.snp_bottom).offset(20)
            make.width.equalTo(70)
            make.height.equalTo(36)
        }
        
        
//        5> 注册按钮 约束
//        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .right, relatedBy: .equal, toItem: msgLable, attribute: .right, multiplier: 1.0, constant: 0))
//        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .top, relatedBy: .equal, toItem: msgLable, attribute: .bottom, multiplier: 1.0, constant: 20))
//        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .width, multiplier: 1.0, constant: 70))
//        addConstraint(NSLayoutConstraint.init(item: registerBtn, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .height, multiplier: 1.0, constant: 36))
        registerBtn.snp.makeConstraints { (make) in
            make.right.equalTo(msgLable.snp.right)
            make.top.equalTo(msgLable.snp.bottom).offset(20)
            make.width.equalTo(70)
            make.height.equalTo(36)
        }
        
//            6> 遮罩图片约束 - 使用中出现问题，先不管，最后再来处理
//        coverImage.snp.makeConstraints { (make) in
//            make.bottom.equalTo(msgLable.snp.bottom)
//            make.width.equalTo(UIScreen.main.bounds.width)
//        }
        /**
         format -> VFL: 可视化格式语言
         H:水平方向
         V:垂直方向
         |：边界
         options:[]：包装控件
         metrics:
         views: 是一个字典，[名字： 控件名] - VFL 字符串中表示控件的字符串
         metrics：是一个字典，[名字： NSNumber] - VFL中表示某个数值
         
         */
        //在self视图上添加coverImgae的约束。 添加toItem后，布局就会被打乱
//        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[cover]-0-|", options: [], metrics: nil, views: ["cover": coverImage, "login": loginBtn]))
//        addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[cover]-(house)-|", options: [], metrics: ["house": -36], views: ["cover": coverImage, "house": houseView]))
        
//        7> 添加圆圈动画
        


    }

    
}
